<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="Hexo Theme Keep">
    <meta name="author" content="cm_westwood">
    
    <title>
        
            第9周 |
        
        西木同学的代码训练 2022
    </title>
    
<link rel="stylesheet" href="/code_training_2022/css/style.css">

    <link rel="shortcut icon" href="">
    
<link rel="stylesheet" href="/code_training_2022/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"cm_westwood.gitee.io","root":"/code_training_2022/","language":"en","path":"search.json"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#0066CC","avatar":"","favicon":"","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"https://xpoet.cn/images/bg.svg","description":"Keep writing and Keep loving."},"scroll":{"progress_bar":{"enable":true},"percent":{"enable":true}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":true,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":true},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"};
  </script>
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
<div class="progress-bar-container">
    
        <span class="scroll-progress-bar"></span>
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                西木同学的代码训练 2022
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/"
                            >
                                HOME
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/archives"
                            >
                                ARCHIVES
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/code_training_2022/tags"
                            >
                                TAGS
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/">HOME</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/archives">ARCHIVES</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/code_training_2022/tags">TAGS</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">第9周</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">cm_westwood</span>
                        
                            <span class="author-label">Lv2</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2022-12-03 20:08:16</span>
        <span class="mobile">2022-12-03 20:08</span>
    </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/code_training_2022/tags/Python/">Python</a>&nbsp;
                    </li>
                
                    <li>
                        | <a href="/code_training_2022/tags/Linux/">Linux</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <blockquote>
<p>最近的广州真冷，关好门窗裹着毛毯睡，早上六点寒气袭来，直接冻醒我了，今天已经下单一个 2.3 kg 的睡袋，准备用这个来过冬了</p>
<p>今天来讲一些 Linux 和 Python 上的杂项吧</p>
</blockquote>
<p>xmodmap 系列工具，用于键盘设置；把字符串输入作为 bash 脚本命令；python setup.py install without sudo；python 里的 GIL 锁；字符串选项的 Python3 类型提示；python 里的单例模式；python 判断 url 地址是否可达；</p>
<span id="more"></span>

<h1 id="xmodmap-系列工具，用于键盘设置"><a href="#xmodmap-系列工具，用于键盘设置" class="headerlink" title="xmodmap 系列工具，用于键盘设置"></a>xmodmap 系列工具，用于键盘设置</h1><p>这个是一个 Linux 上的键盘映射工具，我原本是想用它来魔改一下键位增加工作效率的，例如这里 github 上是有很多和我类似想法的人啊 <a class="link"   target="_blank" rel="noopener" href="https://github.com/search?q=xmodmap+vim%EF%BC%8C%E4%BD%86%E6%98%AF%E6%88%91%E5%AE%9E%E9%99%85%E7%94%A8%E8%B5%B7%E6%9D%A5%E8%BF%98%E6%98%AF%E4%B8%8D%E6%80%8E%E4%B9%88%E8%88%92%E6%9C%8D%E5%95%8A%EF%BC%8C%E6%9C%80%E5%90%8E%E4%B9%9F%E6%98%AF%E4%B8%8D%E4%BA%86%E4%BA%86%E4%B9%8B%E4%BA%86%E3%80%82" >https://github.com/search?q=xmodmap+vim，但是我实际用起来还是不怎么舒服啊，最后也是不了了之了。<i class="fas fa-external-link-alt"></i></a></p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="http://wjhsh.net/brt2-p-14368754.html" >xmodmap系列工具，用于键盘设置<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="把字符串输入作为-bash-脚本命令"><a href="#把字符串输入作为-bash-脚本命令" class="headerlink" title="把字符串输入作为 bash 脚本命令"></a>把字符串输入作为 bash 脚本命令</h1><p>有个工作用的 docker，每次起命令行都要我输入 sudo 密码，不胜其烦，遂用这个方式解放一下劳动力</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 方案一</span></span><br><span class="line"><span class="comment"># The -S switch makes sudo read the password from STDIN. This means you can do</span></span><br><span class="line"><span class="built_in">echo</span> mypassword | sudo -S <span class="built_in">command</span></span><br><span class="line"><span class="comment"># 方案二</span></span><br><span class="line"><span class="comment"># You can set the s bit for your script so that it does not need sudo and runs as root (and you do not need to write your root password in the script):</span></span><br><span class="line">sudo <span class="built_in">chmod</span> +s myscript</span><br></pre></td></tr></table></figure>

<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/11955298/use-sudo-with-password-as-parameter" >linux - Use sudo with password as parameter - Stack Overflow<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="python-setup-py-install-without-sudo"><a href="#python-setup-py-install-without-sudo" class="headerlink" title="python setup.py install without sudo"></a>python setup.py install without sudo</h1><p>我在写一个 python 的打包程序的时候，跟着官方的教程来走 <a class="link"   target="_blank" rel="noopener" href="https://setuptools.pypa.io/en/latest/index.html" >https://setuptools.pypa.io/en/latest/index.html<i class="fas fa-external-link-alt"></i></a> 可遇到了麻烦，打包 python 3.5 的时候并不能用 <code>pip install --upgrade build</code> 这个方法来制作安装包，只能用传统的运行 setup.py 的方式来制作，因为 setup.py 其实也是一个 python 可执行的程序，但是千万要记得：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 这样运行是不对的</span></span><br><span class="line">python setup.py <span class="comment"># 是以 root 用户来安装的，这个时候会提示你 sudo 才行，但是用了 sudo 就不对了，装错人了</span></span><br><span class="line"><span class="comment"># 应该这样 Use the parameter --user to specify your home directory:</span></span><br><span class="line">python setup.py install --user <span class="comment"># 如果不小心运行了 sudo python setup.py，记得一样要用 sudo 卸载</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/43747034/python-setup-py-install-without-sudo" >ubuntu - python setup.py install without sudo - Stack Overflow<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="python-里的-GIL-锁"><a href="#python-里的-GIL-锁" class="headerlink" title="python 里的 GIL 锁"></a>python 里的 GIL 锁</h1><p>目前大家伙下载下来用的 python 底层都是用 C 实现的解释器，即 CPython，由于用 C 实现一个 Python 解释器需要处理复杂的线程安全和并发环境的内存管理问题，为了降低解释器的实现复杂度，CPython 中引入了 GIL(Global Interpreter Lock)。因此，python 的多线程是不能加快计算密集型的程序的哦！因为多线程共享一个 CPython 解释器，无法利用多核的能力，解决方法通常有以下几种：</p>
<ol>
<li>使用 Jython、PyPy 等其他解释器实现</li>
<li>使用多进程代替多线程，这样每个进程可以并行执行</li>
<li>使用 C 等其他语言实现存在性能瓶颈的代码</li>
</ol>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://juejin.cn/post/7007043202251390984" >解除 Python GIL 锁，让代码快 300 倍 - 掘金<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<p>但是 fork 一个进程来对大量数据进行修改，拷贝数据也确实会带来不小的花销，比如说 pandas 的 groupby 操作也是非常耗时的，但至今官方也没有推出一个并行的解决方案，即便我学这里的方法让其开多线程处理，但实际并没有有很大的提升，我觉得是时间是浪费在了进程去拷贝数据上</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://blog.csdn.net/Pual_wang/article/details/106523653" >Pandas groupby加速处理数据问题_potato_big的博客-CSDN博客<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<h1 id="字符串选项的-Python3-类型提示"><a href="#字符串选项的-Python3-类型提示" class="headerlink" title="字符串选项的 Python3 类型提示"></a>字符串选项的 Python3 类型提示</h1><p>如果形参是一个字符串，并且需要让用户知道我可选的字符串有那么几种，就需要在编写代码的时候使用 typing 给用户提示我这个函数的参数可以这么传哦！</p>
<p>You can do that with <a class="link"   target="_blank" rel="noopener" href="https://mypy.readthedocs.io/en/latest/literal_types.html" >literal types<i class="fas fa-external-link-alt"></i></a>.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> typing <span class="keyword">import</span> <span class="type">Literal</span></span><br><span class="line"><span class="comment"># from typing_extensions import Literal # Python 3.7 or below</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">method: <span class="type">Literal</span>[<span class="string">&#x27;simple_method&#x27;</span>, <span class="string">&#x27;some_other_method&#x27;</span>]</span>):</span><br><span class="line">    ...</span><br></pre></td></tr></table></figure>

<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://stackoverflow.com/questions/58114837/python-3-type-hint-for-string-options" >字符串选项的 Python 3 类型提示 - 代码日志<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>
<p>这玩意儿是要用新版的 python 才有的功能哦！python 3.5 可没有这功能</p>
<h1 id="python-里的单例模式"><a href="#python-里的单例模式" class="headerlink" title="python 里的单例模式"></a>python 里的单例模式</h1><p>很多初学者喜欢用<strong>全局变量</strong>，因为这比函数的参数传来传去更容易让人理解。确实在很多场景下用全局变量很方便。不过如果代码规模增大，并且有多个文件的时候，全局变量就会变得比较混乱。你可能不知道在哪个文件中定义了相同类型甚至重名的全局变量，也不知道这个变量在程序的某个地方被做了怎样的操作。</p>
<p>因此对于这种情况，有种更好的实现方式：<br><strong>单例（Singleton）</strong></p>
<p>虽然我写程序现在还没有用上过单例模式，但是我们必须牢记于心有这种解决方案，或许以后会用上。</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/37534850" >Python单例模式(Singleton)的N种实现 - 知乎<i class="fas fa-external-link-alt"></i></a></p>
<p>后记：看了一下评论区</p>
<ul>
<li>显然python的不同文件的全局变量即使重名也没有关系</li>
<li>用JAVA的思想写python</li>
</ul>
<p>说实话，确实我目前为止确实在 Python 里挺少见到要求写单例才能跨过去的坎的，可能确实是 python 的包引用机制做的好</p>
</blockquote>
<h1 id="python-判断-url-地址是否可达"><a href="#python-判断-url-地址是否可达" class="headerlink" title="python 判断 url 地址是否可达"></a>python 判断 url 地址是否可达</h1><p>直接在搜索引擎搜这个问题给的答案都是<code>requests.get(&quot;https://xxx.com&quot;)</code>看返回值是否<code>response.ok == True</code>，不能说这样不行，只能说答对了一半，因为这玩意如果我访问的是一个视频地址，他就会等很久获取完整份视频数据才能返回结果，黄花菜都凉了。</p>
<p>正确的解决方案是只向服务器发送报文头部，不要报文的主体部分，这样就会快</p>
<p>主要实现是通过Request的Body Content Workflow去实现报文头和报文主体分开下载, 即设置参数</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stream=True</span><br></pre></td></tr></table></figure>

<p>实例:</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">req = requests.get(&#x27;https://www.github.com/&#x27;, stream=True))  # 此时实际只向服务器获取了报文头部</span><br><span class="line">content = req.content # 此时才执行剩下报文体的下载</span><br><span class="line"># 或通过迭代器逐行获取</span><br><span class="line">for line in req.iter_lines():</span><br><span class="line">    print line</span><br></pre></td></tr></table></figure>

<p>用这种方式，我们也可以进行分段的大文件下载</p>
<blockquote>
<p><a class="link"   target="_blank" rel="noopener" href="https://blog.flysay.com/python-requests-stream-length.html" >Python Requests判断报文头中Content-Length长度后再下载报文body主体 - Fly Say<i class="fas fa-external-link-alt"></i></a></p>
</blockquote>

        </div>

        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Python/">#Python</a>&nbsp;
                    </li>
                
                    <li class="tag-item">
                        <a href="/code_training_2022/tags/Linux/">#Linux</a>&nbsp;
                    </li>
                
            </ul>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/code_training_2022/2023/01/02/%E7%AC%AC10%E5%91%A8/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">第10周</span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/code_training_2022/2022/11/26/%E7%AC%AC8%E5%91%A8/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">第8周</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">cm_westwood</a>
        </div>
        
        <div class="theme-info info-item">
            Powered by <a target="_blank" href="https://hexo.io">Hexo</a>&nbsp;|&nbsp;Theme&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#xmodmap-%E7%B3%BB%E5%88%97%E5%B7%A5%E5%85%B7%EF%BC%8C%E7%94%A8%E4%BA%8E%E9%94%AE%E7%9B%98%E8%AE%BE%E7%BD%AE"><span class="nav-number">1.</span> <span class="nav-text">xmodmap 系列工具，用于键盘设置</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8A%8A%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BE%93%E5%85%A5%E4%BD%9C%E4%B8%BA-bash-%E8%84%9A%E6%9C%AC%E5%91%BD%E4%BB%A4"><span class="nav-number">2.</span> <span class="nav-text">把字符串输入作为 bash 脚本命令</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#python-setup-py-install-without-sudo"><span class="nav-number">3.</span> <span class="nav-text">python setup.py install without sudo</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#python-%E9%87%8C%E7%9A%84-GIL-%E9%94%81"><span class="nav-number">4.</span> <span class="nav-text">python 里的 GIL 锁</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E9%80%89%E9%A1%B9%E7%9A%84-Python3-%E7%B1%BB%E5%9E%8B%E6%8F%90%E7%A4%BA"><span class="nav-number">5.</span> <span class="nav-text">字符串选项的 Python3 类型提示</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#python-%E9%87%8C%E7%9A%84%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F"><span class="nav-number">6.</span> <span class="nav-text">python 里的单例模式</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#python-%E5%88%A4%E6%96%AD-url-%E5%9C%B0%E5%9D%80%E6%98%AF%E5%90%A6%E5%8F%AF%E8%BE%BE"><span class="nav-number">7.</span> <span class="nav-text">python 判断 url 地址是否可达</span></a></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="Search..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/code_training_2022/js/utils.js"></script>

<script src="/code_training_2022/js/main.js"></script>

<script src="/code_training_2022/js/header-shrink.js"></script>

<script src="/code_training_2022/js/back2top.js"></script>

<script src="/code_training_2022/js/dark-light-toggle.js"></script>



    
<script src="/code_training_2022/js/local-search.js"></script>




    
<script src="/code_training_2022/js/code-copy.js"></script>




    
<script src="/code_training_2022/js/lazyload.js"></script>



<div class="post-scripts">
    
        
<script src="/code_training_2022/js/left-side-toggle.js"></script>

<script src="/code_training_2022/js/libs/anime.min.js"></script>

<script src="/code_training_2022/js/toc.js"></script>

    
</div>



</body>
</html>
